import { Component, OnInit } from '@angular/core';
import { PagedResultDto, HttpService, ExpressionPair, Compare, AndOr, PagerSearchInput } from '../../..';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { async } from '@angular/core/testing';

@Component({
  selector: 'app-student-group-drill-score',
  templateUrl: './student-group-drill-score.component.html',
  styleUrls: ['./student-group-drill-score.component.scss']
})
export class StudentGroupDrillScoreComponent implements OnInit {
  // 项目ID
  selectProjectId;
  // 班级ID
  selectClassId;

  listSearchValue; // 过滤条件
  selectId; // 要修改成绩的条目ID

  score; // 设置报告的成绩。用于弹出窗。

  showScoreModule = false;
  loadingData = false;
  theSubjectFilterType = false;
  /**
 * 分页数据属性
 */
  listEntities = new PagedResultDto<any>(0, 0, 1, 10, new Array<any>());


  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private http: HttpService,
    private _message: NzMessageService,
    private nzModalService: NzModalService
  ) {

  }

  ngOnInit() {
    this.listEntities.pageSize = 12;
    this.route.queryParams.subscribe(e => {
      this.selectProjectId = +e.id;
      this.selectClassId = +e.classid;
      return this.getList();
    });
  }

  getIndex(index: number) {
    index += 1;
    let reslut = index > 4 ? index % 4 : index;
    reslut = reslut === 0 ? reslut + 1 : reslut;
    return reslut;
  }

  async getList() {
    this.loadingData = true;
    this.listSearchValue = new Array<ExpressionPair>(
      new ExpressionPair('projectid', this.selectProjectId, Compare.Equal, AndOr.And),
      new ExpressionPair('classid', this.selectClassId, Compare.Equal, AndOr.And),
      new ExpressionPair('drillType', this.theSubjectFilterType, Compare.Equal, AndOr.And),
    );
    const searchBody = new PagerSearchInput(
      this.listEntities.currentPageIndex,
      this.listEntities.pageSize,
      this.listSearchValue,
      []);
    this.listEntities = await this.http.post<any>(`teacherinfo/AllStudentGroupScore`, searchBody);
    this.loadingData = false;
  }
  /**
   * 关闭班级课程打分。
   */
  async closeSubject(subject: any) {
    this.nzModalService.confirm({
      nzTitle: '提示',
      nzContent: `是否要关闭课程？`,
      nzZIndex: 1010,
      nzOnOk: async () => {
        await this.http.get(`teacherinfo/CloseSubjectTeacherScore?subjectId=${subject.subjectId}&classId=${this.selectClassId}`);
        this.getList();
      }
    });

  }

  // 开始演练
  async startDrill(id) {
    const navigationExtras: NavigationExtras = {
      queryParams: { 'id': id }
    };
    this.router.navigate(['/mydrillcontent'], navigationExtras);
  }
}
